package com.changpay.zywy.ui.activity.serivce

import android.content.Intent
import android.view.View
import com.changpay.zywy.R
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.bean.RateInfo
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.ui.dialog.EditRateDialog
import kotlinx.android.synthetic.main.activity_award.*
import kotlinx.android.synthetic.main.title_layout.*
import java.util.*


class SetAwardActivity : BaseActivity(), IView {


    private var member_id = ""
    private var produce_id = ""
    override fun attachLayoutRes(): Int {
        return R.layout.activity_award
    }

    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {
        showToast(msg)
        finish()
    }

    override fun showMsg(msg: String) {
        showToast(msg)
        finish()
    }

    override fun showError(errorMsg: String) {
        showToast(errorMsg)
        finish()
    }


    override fun initData() {
    }


    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.iv_back -> finish()
            R.id.tv_name -> Intent(this, SelectMemberActivity::class.java).run {
                startActivityForResult(this, 100)
            }
            R.id.tv_select_produce -> {
                if (member_id.isEmpty()) {
                    showToast("请选择修改代理商名称")
                    return@OnClickListener
                }
                Intent(this, SelectProduceActivity::class.java).run {
                    putExtra("member_id", member_id)
                    putExtra("gc_id", intent.getStringExtra("gc_id"))

                    startActivityForResult(this, 100)
                }
            }
            R.id.iv_award_money -> showEditIntDialog()
            R.id.tv_apply -> updateRate()

        }
    }

    override fun initView() {
        tv_title.text = "修改激活奖励金额"
        iv_back.setOnClickListener(onClickListener)
        tv_name.setOnClickListener(onClickListener)
        tv_select_produce.setOnClickListener(onClickListener)
        iv_award_money.setOnClickListener(onClickListener)
        tv_apply.setOnClickListener(onClickListener)

    }

    override fun start() {

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (resultCode) {
            200 -> {
                produce_id = ""
                tv_select_produce.text = ""
                setData(null)
                member_id = data?.getStringExtra("member_id").toString()
                tv_name.text = data?.getStringExtra("member_name").toString()

            }
            300 -> {
                produce_id = data?.getStringExtra("produce_id").toString()
                tv_select_produce.text = data?.getStringExtra("produce_name").toString()
                loadProduceData()
            }
        }
    }

    private fun loadProduceData() {
        val param = hashMapOf<String, String>()
        param["key"] = token
        param["goods_id"] = produce_id
        if (!member_id.isEmpty())
        {
            param["others_member_id"] = member_id
        }
        RetrofitHelper.service.showRateInfo(param).sss(this)
        {
            setData(it.result)
            tv_apply.isEnabled = true
        }


    }

    private var canChange: Boolean = false  // 用于控制是否可以被编辑

    private lateinit var activate_rewards: String



    private fun setData(dateBean: RateInfo?) {
        canChange = if (dateBean != null) {
            tv_award_money.text = dateBean.activate_rewards.toString()
            true
        } else {
            tv_award_money.text = "0"
            false
        }

        tv_award_money_change.text  = ""
        activate_rewards = ""
        tv_apply.isEnabled = false
    }


    private fun showEditIntDialog( ) {
        if (canChange) {
            val title = "激活奖励金额"
            val dialogClickListener = EditRateDialog.OnDialogClickListener { v, inputText ->
                tv_award_money_change.text = ("修改为  " + inputText + "元")
                activate_rewards = inputText
                tv_apply.setEnabled(true)
            }
            val builder = EditRateDialog.Builder(this)
            builder.setInputText("输入值为整数").setTitleText(title).setOnDialogClickListener(dialogClickListener)
            val build = builder.build<EditRateDialog>()
            build.show(supportFragmentManager)
        }
    }


    private fun updateRate() {
        val params = HashMap<String, String>()
        params["key"] = token
        params["others_member_id"] = member_id
        params["goods_id"] = produce_id
        params["activate_rewards"] = activate_rewards
        params["client"] = "android"
        params["gc_id"] = intent.getStringExtra("gc_id")
        RetrofitHelper.service.modifyActivateRewards(params).sss(this)
        {
            showToast(it.result.toString())
            finish()
        }
    }
}
